﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Estimate
{
    public partial class formBudgetWorkCompete : FormMDIChildTemplate
    {
        private Object currentObject;

        public formBudgetWorkCompete(Object currentObject)
        {
            this.currentObject = currentObject;
            InitializeComponent();
        }

        private void formBudgetWorkCompete_Load(object sender, EventArgs e)
        {
            Text = "Виконані Роботи по Об`єкту " + currentObject.ObjectShortName;
            root.TranslateGrid(gridWork);
            root.TranslateGrid(gridSelectedWork);
            root.TranslateGrid(gridAddSum);
            objectWorkCategoryBindingSource.DataSource = currentObject.ObjectWorkCategories.ToArray();

            LoadWork();
            LoadAddSum();
            LoadSelectedWork();
        }

        private void LoadWork()
        {
            if (currentObjectWorkCategory != null)
            {
                var works = from budgetWork in currentObjectWorkCategory.BudgetWorks
                            select new WorkInBudget
                            {
                                Count = budgetWork.cnt,
                                Unit = budgetWork.Work.Unit,
                                WorkID = budgetWork.Work.WorkID,
                                WorkName = budgetWork.Work.WorkName,
                                WorkCode = budgetWork.Work.WorkCode
                            };
                string code = textBoxWorkCode.Text;
                string name = textBoxWorkName.Text;
                if (code != "") works = from x in works where x.WorkCode.Contains(code) select x;
                if (name != "") works = from x in works where x.WorkName.Contains(name) select x;

                List<WorkInBudget> Works = works.ToList (); 
 
                foreach (WorkInBudget workInBudget in Works )
                {
                    decimal cntComplete = currentObjectWorkCategory.ObjectWorks.Where
                        (x => x.WorkID == workInBudget.WorkID).Sum(x => x.cnt);
                    workInBudget.Count -= cntComplete;
                }

                workInBudgetBindingSource.DataSource = Works;
            }
        }

        private void textWork_changed(object sender, EventArgs e)
        { LoadWork(); }


        private void LoadAddSum()
        {
            objectAddSumBindingSource.DataSource = context.ObjectAddSums.Where(x => x.Object == currentObject);
        }

        private void LoadSelectedWork()
        {
            if (currentObjectWorkCategory != null)
            {
                var selectedWorks = from objectWork in currentObjectWorkCategory.ObjectWorks
                                    select new WorkInBudget
                                    {
                                        Count = objectWork.cnt,
                                        DateSet = objectWork.dateset,
                                        Unit = objectWork.Work.Unit,
                                        WorkID = objectWork.ObjectWorkID,
                                        WorkName = objectWork.Work.WorkName
                                    };
                workInBudgetBindingSource1.DataSource = selectedWorks;
            }
        }

        private void buttonWorkAdd_Click(object sender, EventArgs e)
        {
            WorkInBudget workInBudget = (WorkInBudget)workInBudgetBindingSource.Current;
            if (workInBudget != null && currentObjectWorkCategory!=null )
            {
                decimal count = 0;
                try { count = decimal.Parse(textBoxWorkCount.Text); }
                catch { return; }

                decimal [] counts = new decimal[2];
                if (count <= workInBudget.Count) counts[0] = count;
                else
                {
                    counts[0] = workInBudget.Count < 0 ? 0 : workInBudget.Count;
                    counts[1] = workInBudget.Count < 0 ? count : count - workInBudget.Count;
                }

                for (int i = 0; i < 2; i++)
                {
                    decimal cnt = counts[i];
                    if (cnt == 0) continue;
                    ObjectWork objectWork = new ObjectWork();
                    objectWork.cnt = cnt;
                    objectWork.dateset = dateTimePicker.Value;
                    objectWork.IsAdd = i == 0 ? false : true;
                    objectWork.ObjectWorkCategory = currentObjectWorkCategory;
                    objectWork.WorkID = workInBudget.WorkID;
                    context.ObjectWorks.InsertOnSubmit(objectWork);
                    root.Submit();
                }

                LoadWork();
                LoadSelectedWork();
            }
        }

        private void buttonWorkRemove_Click(object sender, EventArgs e)
        {
            WorkInBudget workInBudget = (WorkInBudget)workInBudgetBindingSource1.Current;
            if (workInBudget != null)
            {
                context.ObjectWorks.DeleteAllOnSubmit(context.ObjectWorks.Where(x => x.ObjectWorkID == workInBudget.WorkID));
                root.Submit();
                LoadWork();
                LoadSelectedWork();
            }
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            root.Submit();
            LoadAddSum();
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            ObjectAddSum objectAddSum = (ObjectAddSum)objectAddSumBindingSource.AddNew();
            objectAddSum.Object = currentObject;
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            int year = 2000;
            try { year = int.Parse(textBoxYear.Text ); }
            catch { return; }
            int monthIndex = comboBoxMonth.SelectedIndex;

            Budget budget = new Budget(context, currentObject, year, monthIndex,comboBoxMonth.Text );
            budget.ShowAVR();
            #region trash
            /*
                reportBudgetAct reportBudgetAct = new reportBudgetAct();

             
                reportBudgetAct.xrLabelAgreement.Text = "№ " +  currentObject.ContractNumber +" від " + currentObject.ContractDate.Value.ToLongDateString () ;
                reportBudgetAct.xrLabelBilding.Text = currentObject.ObjectName;

                reportBudgetAct.xrLabelAdd3.Text = budget.AddSum.ToString();

                reportBudgetAct.bindingSourceProduct.DataSource = budget.Products;
                reportBudgetAct.bindingSourceWork.DataSource = budget.Works;

                reportBudgetAct.xrLabelFirm1.Lines = currentObject.Firm.GetFullDescription();
                reportBudgetAct.xrLabelFirm2.Lines = db.GetFirmOwn().GetFullDescription();
             //   reportBudgetAct.xrLabelObject.Text = currentObject.ObjectName;

                reportBudgetAct.xrLabelPart1.Text = budget.Part1.ToString();
                reportBudgetAct.xrLabelPart1Total.Text = budget.Part1Total.ToString();
                reportBudgetAct.xrLabelTaxCurrent.Text = budget.TaxCurrent.ToString();
                reportBudgetAct.xrLabelTaxCurrentP.Text = currentObject.TaxCurrent.ToString();

                reportBudgetAct.xrLabelPart2.Text = budget.Part2.ToString();
                reportBudgetAct.xrLabelTaxStorehouse.Text = budget.TaxStorehouse.ToString();
                reportBudgetAct.xrLabelTaxStorehouseP.Text = currentObject.TaxStorehouse.ToString();
                reportBudgetAct.xrLabelTaxTransport.Text = budget.TaxTransport.ToString();
                reportBudgetAct.xrLabelTaxTransportP.Text = currentObject.TaxTransport.ToString();
                reportBudgetAct.xrLabelPart2Total.Text = budget.Part2Total.ToString();
                reportBudgetAct.xrLabelPart12.Text = budget.Part12.ToString();

                reportBudgetAct.xrLabelTaxAdmin.Text = budget.TaxAdmin.ToString();
                reportBudgetAct.xrLabelTaxAdminP.Text = currentObject.TaxAdmin.ToString();
                reportBudgetAct.xrLabelTaxProfit.Text = budget.TaxProfit.ToString();
                reportBudgetAct.xrLabelTaxProfitP.Text = currentObject.TaxProfit.ToString();
                reportBudgetAct.xrLabelPart12Total.Text = budget.Part12Total.ToString();

                reportBudgetAct.xrLabelTaxSingle.Text = budget.TaxSingle.ToString();
                //reportBudgetAct.xrLabelTaxSingleP.Text = currentObject.TaxSingle.ToString();
                reportBudgetAct.xrLabelTotal.Text = budget.Total.ToString();
                reportBudgetAct.xrLabelTaxVat.Text = budget.TaxVat.ToString();
                reportBudgetAct.xrLabelTaxVatP.Text = currentObject.TaxVat.ToString();
                reportBudgetAct.xrLabelTotalTotal.Text = budget.TotalTotal.ToString();
                reportBudgetAct.xrLabelZamovnyk.Text = currentObject.Firm.Director;
                reportBudgetAct.xrLabelPidryadnyk.Text = db.GetFirmOwn().Director;


                reportBudgetAct.ShowPreview();
                 */
            #endregion

        }


        ObjectWorkCategory currentObjectWorkCategory;
        private void objectWorkCategoryBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            currentObjectWorkCategory = (ObjectWorkCategory)objectWorkCategoryBindingSource.Current;
            if (currentObjectWorkCategory != null)
            {
                LoadWork();
                LoadSelectedWork();
            }
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {

        }

        private void saveToolStripButton_Click_1(object sender, EventArgs e)
        {
            root.Submit();
        }
    }
}
